package app.dev.ems.data.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;

import app.dev.ems.data.dao.IBaseDao;
import app.dev.ems.data.model.impl.Base;

public abstract class BaseDaoImpl<T extends Base> implements IBaseDao<T> {

	private Class<T> entityClass;
	private SessionFactory sessionFactory;
		
	public BaseDaoImpl(Class<T> entityClass) {
		super();
		this.entityClass = entityClass;
	}	

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}	

	public Integer save(T entity) {
		return (Integer) getSessionFactory().getCurrentSession().save(entity);
	}
	
	public void update(T entity) {
		getSessionFactory().getCurrentSession().update(entity);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> get(Criterion criterion) {
		return getSessionFactory().getCurrentSession().createCriteria(entityClass).add(criterion).list();
	}
	
	@SuppressWarnings("unchecked")
	public T get(int id) {
		return (T) getSessionFactory().getCurrentSession().get(entityClass, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getAll() {
		return getSessionFactory().getCurrentSession().createCriteria(entityClass).list();
	}
}
